<template>
  <div class="container">
    <div class="form-group">
      <label for="exampleInputEmail1">添加待办事项</label>
      <input type="text" class="form-control"
             id="exampleInputEmail1" v-model="inputValue" @keydown.enter="add(inputValue)">
      <small id="emailHelp" class="form-text text-muted">回车即添加</small>
    </div>
    <ul class="list-group">
      <li class="list-group-item" v-for="(item,index) in todos" :key="index" style="overflow: hidden">
        <div class="form-check" style="float: left">
          <input type="checkbox" class="form-check-input" id="exampleCheck1" @click.prevent="doneList(index)">
          <label class="form-check-label" for="exampleCheck1">{{item}}</label>
        </div>
        <button class="btn btn-danger" type="button" style="float: right" @click="delList(index)">删除</button>
      </li>
    </ul>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, reactive, computed } from 'vue'
import store from '@/store'

export default defineComponent({
  setup () {
    const inputValue = ref('')
    const add = (value: string) => {
      if (value.trim() === '') return
      store.commit('addList', value)
      inputValue.value = ''
    }
    const delList = (value: number) => {
      store.commit('delList', value)
    }
    const doneList = (value: number) => {
      store.commit('doneList', value)
    }
    return reactive({
      inputValue,
      todos: computed(() => store.state.todoList),
      add,
      delList,
      doneList
    })
  }
})
</script>

<style scoped>

</style>
